<template>
    <el-dialog
            :visible.sync="visible"
            width="700px"
            :before-close="handleClose"
            class="box-card">
        <div class="title">
            <i class="el-icon-edit"> {{ title }}</i>
        </div>
        <el-form
                ref="formRef"
                :rules="rules"
                :model="formData"
                label-width="100px"
                label-position="right"
                style="width: 90%"
                status-icon>
            <el-form-item label="名称" prop="name">
                <el-input v-model.trim="formData.name" maxlength="30" clearable show-word-limit/>
            </el-form-item>
            <el-form-item label="拼音" prop="pinyin">
                <el-input v-model.trim="formData.pinyin" maxlength="50" clearable show-word-limit/>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input-number v-model.trim="formData.sort" :min="1" :max="1000">
                </el-input-number>
            </el-form-item>
            <el-form-item label="描述" prop="description">
                <el-input type="textarea" :rows="2" v-model.trim="formData.description" maxlength="100" clearable
                          show-word-limit/>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model.trim="formData.status" clearable style="width:185px">
                    <el-option label="正常" value="1"></el-option>
                    <el-option label="已删除" value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item align="right">
                <el-button @click="handleClose" size="mini">取消</el-button>
                <el-button @click="resetForm('formRef')" size="mini">重置</el-button>
                <el-button type="primary" @click="submitForm('formRef')" size="mini">确定</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
    import {addCategory, updateCategory} from '@/api/api';

    export default {
        props: {
            title: { // 弹窗的标题
                type: String,
                default: ''
            },
            visible: { // 弹出窗口，true弹出
                type: Boolean,
                default: false
            },
            formData: { // 提交表单数据对象
                type: Object,
                default: () => { // 对象或数组默认值必须从一个工厂函数获取
                    return {}
                }
            },
            remoteClose: Function // 用于关闭窗口
        },
        data() {
            return {
                // 校验表单数据
                rules: {
                    name: [
                        {required: true, message: '分类名不能为空', trigger: 'blur'},
                        {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur'}
                    ],
                    pinyin: [
                        {required: true, message: '拼音不能为空', trigger: 'blur'},
                        {min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur'}
                    ],
                    description: [
                        {required: true, message: '描述不能为空', trigger: 'blur'},
                        {min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            /**
             * 取消(关闭窗口,清空表单和校验结果)
             */
            handleClose() {
                this.$refs['formRef'].resetFields()
                // 注意不可以通过  this.visible = false来关闭，因为它是父组件的属性
                this.remoteClose()
            },
            /**
             * 重置表单
             * @param formName
             */
            resetForm(formName) {
                this.$refs[formName].resetFields()
            },
            /**
             * 提交表单数据
             * @param formName
             */
            submitForm(formRef) {
                this.$refs[formRef].validate((valid) => {
                    if (valid) {
                        // 校验通过，提交表单数据
                        console.log('111111')
                        this.addOrUpdate()
                    } else {
                        return false
                    }
                })
            },
            // 添加或更新
            async addOrUpdate() {
                let response = null

                if (this.formData.id) {
                    // 有 id 为更新
                    response = await updateCategory(this.formData)
                    if (response.code === 20000) {
                        this.$message.success('更新分类成功')
                        // 关闭窗口
                        this.handleClose()
                    } else {
                        this.$message.error(response.message)
                    }
                } else {
                    // 没 id 为添加
                    response = await addCategory(this.formData)
                    if (response.code === 20000) {
                        this.$message.success('添加分类成功')
                        // 关闭窗口
                        this.handleClose()
                    } else {
                        this.$message.error(this.formData)
                    }
                }
            }
        }
    }
</script>
<style scoped>
    .title {
        font-size: 22px;
        text-align: center;
        margin-bottom: 20px;
    }
</style>